package com.itheima.config;

import com.itheima.controller.backend.security.SpringSecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

/**
 * Security配置类
 */

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    /**
     * http请求处理方法
     *
     * @param http
     * @throws Exception
     */

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/backend/login.html")   //配置登录页面
                .loginProcessingUrl("/login")   //登录请求处理路径
                .usernameParameter("username")  //用户名和密码参数
                .passwordParameter("password")
                .defaultSuccessUrl("/backend/pages/main.html")  //登录成功后跳转到这个页面
                .and()
                .authorizeRequests()
                .antMatchers("/backend/login.html", "/login").permitAll()   //permitAll()允许所有用户访问
                .antMatchers("/checkitem/**").permitAll()  // 确保这行在 .authenticated() 之前
                .antMatchers("/backend/**").authenticated() //authenticated()需要认证后才能访问
                .antMatchers("/setmeal/**", "/checkgroup/**").permitAll()
                .anyRequest().permitAll();  // 修改为 permitAll(),其他所有请求允许所有用户访问

        // csrf防护
        http.csrf().disable();
        
        // 允许iframe加载页面
        http.headers().frameOptions().sameOrigin();

        // 添加跨域支持
        http.cors();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/backend/css/**",
                "/backend/img/**",
                "/backend/js/**",
                "/backend/plugins/**",
                "/favicon.ico"
        );
    }

    @Autowired
    private SpringSecurityUserService userService;

    // 配置密码加密
    @Bean
    protected PasswordEncoder myPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(myPasswordEncoder());
    }

    // 添加跨域配置
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("*");  // 允许所有来源
        configuration.addAllowedMethod("*");  // 允许所有HTTP方法
        configuration.addAllowedHeader("*");  // 允许所有头部
        configuration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

